<template>
  <div>
    <el-col>
      <el-tag>文章总数:{{ article.total }}</el-tag>
    </el-col>
    <el-table
        :data="article.records"
        style="width: 100%"
    >
      <el-table-column
          fixed
          prop="username"
          label="用户名"
          width="150"
      >
      </el-table-column>
      <el-table-column
          prop="userImg"
          label="用户头像"
          width="120"
      >
        <template slot-scope="scope">
          <el-avatar :src="scope.row.userImg"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column
          prop="articleTitle"
          label="文章标题"
          width="150"
      >
      </el-table-column>
      <el-table-column
          prop="articleImage"
          label="课程封面图片"
          width="150"
      >
        <template slot-scope="scope">
          <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.articleImage"
              fit="fill"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="articleGood"
          label="点赞数"
          width="50"
      >
      </el-table-column>

      <el-table-column
          prop="articleBad"
          label="踩"
          width="50"
      >
      </el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
      >
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleClick(scope.row)">查看文章内容</el-button>
          <el-button size="small" type="danger" @click="handleReject(scope.row)">驳回文章信息</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除文章信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        :current-page="article.current"
        :page-size="article.size"
        @current-change="change"
        layout="prev, pager, next"
        :total="article.total"
    >
    </el-pagination>

    <el-dialog
        title="查看文章"
        :visible.sync="showdialogVisible"
        width="30%"
    >
      <span>{{ content }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showdialogVisible = false">取 消</el-button>
  </span>
    </el-dialog>


    <el-dialog
        title="删除文章"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <span>删除文章</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
         <el-button @click="Delete" type="danger">确定删除</el-button>
  </span>
    </el-dialog>

    <el-dialog
        title="驳回文章"
        :visible.sync="rejectVisible"
        width="30%"
    >
      <span>{{ content }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rejectVisible = false">取 消</el-button>
         <el-button @click="reject">确定驳回</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {selectAllArticle, deleteArticle, rejectArticle} from '@/api/article'

export default {

  name: 'Index',
  data() {
    return {
      article: [],
      id: '',
      showdialogVisible: false,
      dialogVisible: false,
      rejectVisible: false,
      content: ''
    }
  },
  mounted() {
    this.change(1)
  },
  methods: {
    //
    reject() {
      rejectArticle(this.id).then(result => {
        console.log(result)
        this.change(1)
        this.$message({
          type: 'success',
          duration: 2 * 1000,
          message: result.data.article
        })
      }).catch(error => {
        this.$message({
          type: 'error',
          duration: 2 * 1000,
          message: error.message
        })
      })
      this.rejectVisible = false
    },
    /**
     * 点击驳回文章安妮
     *
     **/
    handleReject(row) {
      this.id = row.id
      this.rejectVisible = true
    },
    // 点击查看文章信息按钮
    handleClick(row) {
      this.content = row.articleContext
      this.showdialogVisible = true
    },
    /**
     *  删除文章信息
     * @param row
     */
    handleDelete(row) {
      this.id = row.id;
      this.dialogVisible = true
    },
    Delete() {
      deleteArticle(this.id).then(result => {
        this.change(1)
        this.$message({
          type: 'success',
          duration: 2 * 1000,
          message: result.data.article
        })
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error.message,
          duration: 2 * 1000
        })
      })
    },
    // 查找文章信息
    change(current) {
      selectAllArticle(current).then(result => {
        this.article = result.data.article
      }).catch(error => {
        this.$message({
          type: 'error',
          duration: 2 * 1000,
          message: error.message
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
